package com.jiangy.justauth.controller;

import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>创建时间: 2024/8/12 </p>
 *
 * @author <a href="mailto:jiangliu0316@outlook.com" rel="nofollow">蒋勇</a>
 */
@RestController
@RequestMapping
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello justauth";
    }

    @GetMapping("/oauth2/authorize")
    public String getAuthorize(String provider) {
        return create(provider);
    }

    @GetMapping("/login/oauth2/code/gitee")
    public Object login(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest();
        return authRequest.login(callback);
    }

    private String create(String provider) {
        if ("gitee".equals(provider)) {
            return getAuthRequest().authorize(AuthStateUtils.createState());
        }
        return "";
    }

    private AuthRequest getAuthRequest() {
        return new AuthGiteeRequest(AuthConfig.builder()
                .clientId("5ce6631ee832418373ebbd3ab00c2a4c453c5bec49bd93ceca30a3fff4f5b91e")
                .clientSecret("d372939bbdfd42871a588849f3d8ef434661eaeed40076e6452aff12364c1b18")
                .redirectUri("http://meunsc.oicp.net/login/oauth2/code/gitee")
                .build());
    }
}
